/**
 * 
 */
package classes;

import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JOptionPane;

/**
 * Classe que corresponde a tela de cadastro do sistema, usando interface grafica.
 * @author equipe
 *
 */
public class TelaCadastro extends javax.swing.JFrame {


	private static final long serialVersionUID = 1L;
	private Sala sala;
	private Professor professor;
	private javax.swing.JButton botaoSalvar;
	private javax.swing.JButton botaoCancelar;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JTextField campoNome;
	private javax.swing.JTextField campoMatricula;
	private javax.swing.JTextField campoBloco;
	private javax.swing.JTextField campoSala;

	/**
	 * Cria uma tela de cadastro.
	 */
	public TelaCadastro() {
		initComponents();
	}

	/**
	 * Componentes que compoe a tela de cadastro e seus campos, onde o usuario digita seus dados.
	 */
	private void initComponents() {

		jLabel4 = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		botaoSalvar = new javax.swing.JButton();
		campoNome = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		campoMatricula = new javax.swing.JTextField();
		campoBloco = new javax.swing.JTextField();
		campoSala = new javax.swing.JTextField();
		botaoCancelar = new javax.swing.JButton();

		jLabel4.setText("jLabel4");

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Cadastro - Biblioteca");
		setResizable(false);

		jLabel1.setText("Nome Completo");

		campoNome.addFocusListener(new java.awt.event.FocusAdapter() {
			/**
			 * Evento acionado pela iniciativa do usuario de deixar o campo nome pronto para uso.
			 */
			public void focusLost(java.awt.event.FocusEvent evt) {
				campoNomeFocusLost(evt);
			}
		});

		botaoSalvar.setText("Salvar");
		botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					botaoSalvarActionPerformed(evt);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		botaoCancelar.setText("Cancelar");
		botaoCancelar.addActionListener(new java.awt.event.ActionListener() {

			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				// TODO Auto-generated method stub
				try {
					botaoCancelarActionPerformed(evt);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});

		campoMatricula.addFocusListener(new java.awt.event.FocusAdapter() {
			/**
			 * Evento acionado pela iniciativa do usuario de deixar o campo matricula pronto para uso.
			 */
			public void focusLost(java.awt.event.FocusEvent evt) {
				campoMatriculaFocusLost(evt);
			}
		});

		campoBloco.addFocusListener(new java.awt.event.FocusAdapter() {
			/**
			 * Evento acionado pela iniciativa do usuario de deixar o campo bloco pronto para uso.
			 */
			public void focusLost(java.awt.event.FocusEvent evt) {
				campoBlocoFocusLost(evt);
			}
		});

		campoSala.addFocusListener(new java.awt.event.FocusAdapter(){
			/**
			 * Evento acionado pela iniciativa do usuario de deixar o campo sala pronto para uso.
			 */
			public void focusLost(java.awt.event.FocusEvent evt) {
				campoSalaFocusLost(evt);
			}
		});


		jLabel2.setText("Bloco");

		jLabel3.setText("Sala");

		jLabel5.setText("Matricula");



		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup()
										.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(layout.createSequentialGroup()
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(layout.createSequentialGroup()
																.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
																		.addGroup(layout.createSequentialGroup()
																				.addComponent(jLabel3)
																				.addGap(49, 49, 49))
																				.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
																						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
																								.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
																								.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
																								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
																								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																										.addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addComponent(campoBloco, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addComponent(campoSala, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
																										.addGap(38, 38, 38))
																										.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
																												.addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addGap(29, 29, 29)))
																												.addComponent(botaoCancelar)))
																												.addContainerGap(26, Short.MAX_VALUE))
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGap(21, 21, 21)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(jLabel5)
										.addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(campoBloco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(campoSala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGap(18, 18, 18)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(botaoSalvar)
																.addComponent(botaoCancelar))
																.addGap(25, 25, 25))
				);

		pack();
	}

	/**
	 * Configura o que o usuario digitou para o campo nome.
	 * @param evt
	 * 		Parametro a ser configurado para o campo nome.
	 */
	private void campoNomeFocusLost(java.awt.event.FocusEvent evt) {
		Biblioteca.setTxtNome(campoNome.getText());

	}

	/**
	 * Configura o que o usuario digitou para o campo matricula.
	 * @param evt
	 * 		Parametro a ser configurado para o campo matricula.
	 */
	private void campoMatriculaFocusLost(java.awt.event.FocusEvent evt) {
		// TODO Auto-generated method stub
		Biblioteca.setTxtMatricula(campoMatricula.getText());

	}

	/**
	 * Configura o que o usuario digitou para o campo bloco.
	 * @param evt
	 * 		Parametro a ser configurado para o campo bloco.
	 */
	private void campoBlocoFocusLost(FocusEvent evt) {
		// TODO Auto-generated method stub
		Biblioteca.setTxtBloco(campoBloco.getText());


	}

	/**
	 * Configura o que o usuario digitou para o campo sala.
	 * @param evt
	 * 		Parametro a ser configurado para o campo sala.
	 */
	private void campoSalaFocusLost(FocusEvent evt) {
		// TODO Auto-generated method stub
		Biblioteca.setTxtSala(campoSala.getText());

	}

	/**
	 * Salva os dados que o usuario digitou nos campos da tela de cadastro.
	 * @param evt
	 * 		Parametro que serve de controle para manusear os metodos da biblioteca.
	 * @throws Exception
	 * 		Lanca excecao, caso o usuario digite alguma campo de maneira errada.
	 */
	private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt){

		try {
			sala = new Sala(Biblioteca.getTxtBloco(), Biblioteca.getNumeroDaSala());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			return;
		}

		try {
			professor = new Professor(Biblioteca.getTxtNome(), Biblioteca.getTxtMatricula(), sala);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			//JOptionPane.showMessageDialog(null, "Preencha todos os campos");
			return;

		}	
		
		try {
			Biblioteca.adicionaProfessor(professor);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String login = JOptionPane.showInputDialog("Digite o Login:");
		String senha = JOptionPane.showInputDialog("Digite a senha:");

		try {
			professor.setLogin(login, senha);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			return;
		}

		Biblioteca.telaDeCadastro.dispose();
		professor.setTelaDeUsuario(new TelaDeUsuario(professor));
		professor.telaDeUsuario.setLocationRelativeTo(null);
		professor.telaDeUsuario.setVisible(true);

	}

	/**
	 * Cancela a tela de cadastro e volta para a tela de usuario.
	 * @param evt
	 * 		Parametro que serve de controle para manusear os metodos da biblioteca.
	 */
	private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt){
		Biblioteca.telaDeCadastro.dispose();
		Biblioteca.telaDeLogin.setVisible(true);
	}
}